package com.ezops.poc.bo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ezops.poc.dao.UtilityDAO;

@Component
public class AssetLookUp {

	@Autowired
	private UtilityDAO dao;

	private static Map<String, Integer> mapping = null;

	public int lookUp(String key) {

		return mapping.get(key.toLowerCase());
	}
	
	public String reverseLookUp(int lookUpAssetId) {
		for(Entry<String, Integer> entry:mapping.entrySet()){
			if(entry.getValue().equals(lookUpAssetId))
				return entry.getKey();
		}
		return "NA";
	}

	@PostConstruct
	public void load() {
		mapping = new HashMap<>();
		List<Asset> assets = dao.getAllAssets();
		for (Asset asset : assets) {
			mapping.put(asset.getAssetName().toLowerCase(), asset.getAssetId());
		}
	}

}
